package com.cg.bank.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @Description MD5加密
 * @author chunges
 * @date 2018年7月15日 下午5:30:31
 * @version 1.0
 */

public class MD5 {
	private MD5 () {
	}
	
	private static String encode(byte[] source) {
		String s = null;
		char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
		try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			md.update(source);
			byte tmp[] = md.digest();
			char str[] = new char[16 * 2];
			int k = 0;
			byte byte0;
			for (int i = 0; i < 16; i++) {
				byte0 = tmp[i];
				str[k++] = hexDigits[byte0 >>> 4 & 0xf];
				str[k++] = hexDigits[byte0 & 0xf];
			}
			s = new String(str);
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		
		return s;
	}

	public static String getMD5(String source) {
		return (source == null || "".equals(source)) ? "" : encode(source.getBytes());
	}
	
}